package za.ac.wits.elen7045.persistence.model;

import java.util.LinkedList;
import java.util.List;
import za.ac.wits.elen7045.persistence.PersistentObject;
import za.ac.wits.elen7045.persistence.exception.ObjectAlreadyExistException;
import za.ac.wits.elen7045.persistence.exception.ObjectNotFoundException;

public class User extends PersistentObject {

	private String name;
	private String username;
	private String password;
	private List<Account> accounts;
	
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public List<Account> getAccounts() {
		return accounts;
	}

	public List<Account> getAccounts(ServiceProviderName serviceProviderType) {
		List<Account> accountsByType = new LinkedList<Account>();
		for (Account account : accounts) {
			if (account.getServiceProviderType() == serviceProviderType) {
				accountsByType.add(account);
			}
		}
		return accountsByType;
	}
	
	public void setAccounts(List<Account> accounts) {
		this.accounts = accounts;
	}

	public Account getAccount(long accountNumber) {
		for (Account account : accounts) {
			if (account.getAccountNumber() == accountNumber) {
				return account;
			}
		}
		return null;
	}
	
	public void addAccount(ServiceProviderName serviceProviderType, int accountNumber, String accountHolderName) {
		if (getAccount(accountNumber) != null) {
			throw new ObjectAlreadyExistException();
		} else {
			Account account = new Account(serviceProviderType, accountNumber);
			account.setAccountOwnerName(accountHolderName);
			accounts.add(account);
		}
	}
	
	public void addAccount(Account account) {
		if (accounts.contains(account)) {
			throw new ObjectAlreadyExistException();
		} else {
			accounts.add(account);
		}
	}
	
	public void removeAccount(int accountNumber) {
		Account account = getAccount(accountNumber); 
		if (account == null) {
			throw new ObjectNotFoundException();
		} else {
			accounts.remove(account);
		}
	}
	
	public void removeAccount(Account account) {
		if (!accounts.contains(account)) {
			throw new ObjectNotFoundException();
		} else {
			accounts.remove(account);
		}
	}
	
}
